//
// Created by superzou on 2019/11/6.
//

#include "MainWindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QFileDialog>
#include <QMessageBox>
#include <QImage>
#include <QFileInfo>
#include <QPushButton>
#include <QGraphicsScene>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "mylabel.h"
#include "positionlineedit.h"
MainWindow::MainWindow(QWidget *parent)
    :QMainWindow (parent),
      ui(new Ui::MainWindow)

{

    ui->setupUi(this);


    connect(ui->pushButton, &QPushButton::clicked,
            this, &MainWindow::openImage);
    connect(ui->image, &MyLabel::currentPosition,
            ui->x_input,&PositionLineEdit::receivePosition);
    connect(ui->image,&MyLabel::sendMask,
            ui->mask, &DisplayLabel::loadImage);
}

MainWindow::~MainWindow() {
delete ui;
}

bool MainWindow::openImage()
{
    QString fileName =
            QFileDialog::getOpenFileName(this,
                                         tr("open image file"),
                                         "./",
                                         tr("Image files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm);;All files (*.*)")
                                          );
    if(fileName.isEmpty())
       {
           QMessageBox mesg;
           mesg.warning(this,"警告","打开图片失败!");
           return false;
       }
    QFileInfo openFileInfo = QFileInfo(fileName);
    QString imagePath = openFileInfo.filePath();

    ui-> lineEdit->setText(imagePath);
    image = cv::imread(imagePath.toStdString());
    cv::cvtColor(image, image, CV_BGR2RGB);
    cv::Mat mask = cv::Mat::zeros(image.size(), CV_8UC1);
    ui->image->clearImage();
    ui->image->loadImage(image);
    ui->image->addImage(image);
    ui->image->addImage(mask);
    return true;
}

